﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class user_index : System.Web.UI.Page
{
    private ProductBus ProductBus = new ProductBus();
    private int cat_id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["categoryID"] == null)
            {
                LoadAllProduct();
            }
            else
            {
                cat_id = Convert.ToInt32(Request.QueryString["categoryID"]);
                LoadProductByCategory(cat_id);
            }
        }

    }
    public void LoadAllProduct()
    {
        ClPageProduct.BindToControl = dtListProduct;
        ClPageProduct.DataSource = ProductBus.GetAllProduct().DefaultView;

        dtListProduct.DataSource = ClPageProduct.DataSourcePaged;
        dtListProduct.DataBind();
    }

    public void LoadProductByCategory(int id)
    {
        ClPageProduct.BindToControl = dtListProduct;
        ClPageProduct.DataSource = ProductBus.GetProductByCategory(id).DefaultView;

        dtListProduct.DataSource = ProductBus.GetProductByCategory(id).DefaultView;
        dtListProduct.DataBind();
    }

    protected void ImageButton1_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "AddToCart")
        {
            int idProduct = Convert.ToInt32(e.CommandArgument);
            ShoppingCart.Instance.AddItem(idProduct);
            Response.Redirect("shoppingcart.aspx");
        }
    }
    protected void imgSearch_Click(object sender, ImageClickEventArgs e)
    {

        if (drtextsearch.SelectedIndex == 0)
        {
            ClPageProduct.BindToControl = dtListProduct;
            ClPageProduct.DataSource = ProductBus.SearchProduct(txtSearch.Text).DefaultView;

            dtListProduct.DataSource = ClPageProduct.DataSourcePaged;
            dtListProduct.DataBind();
        }
        else if (drtextsearch.SelectedIndex == 1)
        {
            ClPageProduct.BindToControl = dtListProduct;
            int Id = Convert.ToInt32(txtSearch.Text);
            ClPageProduct.DataSource = ProductBus.GetProductByID(Id).DefaultView;
            dtListProduct.DataSource = ClPageProduct.DataSourcePaged;
            dtListProduct.DataBind();

        }
        else if (drtextsearch.SelectedIndex == 2)
        {
            ClPageProduct.BindToControl = dtListProduct;
            decimal money = Convert.ToDecimal(txtSearch.Text);
            ClPageProduct.DataSource = ProductBus.SearchProductByPrice(money).DefaultView;
            dtListProduct.DataSource = ClPageProduct.DataSourcePaged;
            dtListProduct.DataBind();
        }

    }
    
}